import { observable, action } from 'mobx'
import * as api from 'lib/constants/api'
import { post } from 'lib/net'
import Toast from 'components/toast'
import Load from 'components/load'

class AddressModel {
  constructor (data) {
    if (data) {
      this.label = data.Name
      this.value = data.AreaCode
      this.children = []
      if (data.ChildList) {
        data.ChildList.map(item => {
          this.children.push(new AddressModel(item))
        })
      }
    }
  }
}

export default class AddProjectVM {
  constructor (props) {
    this.props = props
    this.PageInitialization()
    if (this.props.history.location.state) {
      let obj = this.props.history.location.state.name
      this.companyName = obj.CompanyName
      this.userName = obj.LinkName
      this.userNumber = obj.LinkPhone
      this.cityCode = [obj.Province, obj.City]
      this.addressDetail = obj.Address
      this.serviceTotal = obj.ServiceTotal
      this.service = obj.Service
      this.videoTotal = obj.VideoTotal
      this.video = obj.Video
      this.memoryTotal = obj.MemoryTotal
      this.memory = obj.Memory
      this.projectId = obj.Id
      this.editData = obj
    } else {
      return false
    }
  }

  // 服务价格
  @observable servicePrice = ''

  // 项目Id
  @observable projectId = ''

  // 摄像头价格
  @observable videoPrice = ''

  // 内存卡价格
  @observable memoryPrice = ''

  // 总价
  @observable total = '0.00'

  // 服务时长
  @observable service = ''

  // 服务时长总额
  @observable serviceTotal = ''

  // // 服务时长总价
  // @observable byDays = '0.00'

  // 摄像头个数
  @observable video = ''

  // 摄像头个数总价
  @observable videoTotal = ''

  // // 摄像头总价
  // @observable byCount = '0.00'

  // 内存卡个数
  @observable memory = ''

  // 内存卡个数总价
  @observable memoryTotal = ''

  // // 内存卡总价
  // @observable bySheets = '0.00'

  // 公司名称
  @observable companyName = ''

  // 联系人
  @observable userName = ''

  // 手机号
  @observable userNumber = ''

  // 选择地区
  @observable cityCode = []

  // 详细地址
  @observable addressDetail = ''

  // 获取城市列表
  @observable addressList = []

  // 洽谈中跳转带值的编辑页面
  @observable editData = {}

  // 选择城市样式
  @observable addressStyle = ''

  // 获取页面数据
  @action onChange = (v, key) => {
    switch (key) {
      case 'CompanyName':
        this.companyName = v
        break
      case 'UserName':
        this.userName = v
        break
      case 'UserNumber':
        this.userNumber = v
        break
      case 'CityCode':
        this.cityCode = v
        this.addressStyle = 'addressStyle'
        break
      case 'AddressDetail':
        this.addressDetail = v
        break
      case 'ByDays':
        this.service = v
        // this.byDays = (v * this.servicePrice).toFixed(2)
        // this.total = (Number(this.byDays) + Number(this.byCount) + Number(this.bySheets)).toFixed(2)
        break
      case 'ByDaysTotal':
        this.serviceTotal = v
        // this.byDays = (v * this.servicePrice).toFixed(2)
        this.total = (Number(this.serviceTotal) + Number(this.videoTotal) + Number(this.memoryTotal)).toFixed(2)
        break
      case 'ByCount':
        this.video = v
        // this.byCount = (v * this.videoPrice).toFixed(2)
        // this.total = (Number(this.byDays) + Number(this.byCount) + Number(this.bySheets)).toFixed(2)
        break
      case 'ByCountTotal':
        this.videoTotal = v
        // this.byCount = (v * this.videoPrice).toFixed(2)
        this.total = (Number(this.serviceTotal) + Number(this.videoTotal) + Number(this.memoryTotal)).toFixed(2)
        break
      case 'BySheets':
        this.memory = v
        // this.bySheets = (v * this.memoryPrice).toFixed(2)
        // this.total = (Number(this.byDays) + Number(this.byCount) + Number(this.bySheets)).toFixed(2)
        break
      case 'BySheetsTotal':
        this.memoryTotal = v
        // this.bySheets = (v * this.memoryPrice).toFixed(2)
        this.total = (Number(this.serviceTotal) + Number(this.videoTotal) + Number(this.memoryTotal)).toFixed(2)
        break
    }
  }

  // 验证手机号是否符合正则
  @action phoneCodeValid = (number) => {
    this.number = number
    this.number = Number(this.myTrim(this.number, 'g'))
    let myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
    if (!myreg.test(this.number)) {
      Toast.fail('请输入正确手机号')
      return false
    } else {
      return true
    }
  }

  // 去除字符串中的空格
  @action myTrim = (str, global) => {
    let result
    result = str.toString().replace(/(^\s+)|(\s+$)/g, '')
    if (global.toLowerCase() === 'g') {
      result = result.replace(/\s/g, '')
    }
    return result
  }

  // 提交项目公共部分封装
  @action common = async (applyStatus, state) => {
    if (this.myTrim(this.companyName, 'g') === '') {
      Toast.fail('公司名称不能为空')
    } else if (this.myTrim(this.userName, 'g') === '') {
      Toast.fail('联系人不能为空')
    } else if (this.phoneCodeValid(this.userNumber) === false) {
      Toast.fail('联系电话不能为空')
    } else if (this.cityCode[0] === undefined) {
      Toast.fail('所选地区不能为空')
    } else if (this.addressDetail === '') {
      Toast.fail('详细地址不能为空')
    } else {
      // else if (this.service === '') {
      //   Toast.fail('服务时长不能为空')
      // } else if (this.serviceTotal === '') {
      //   Toast.fail('时长总额不能为空')
      // } else if (this.video === '') {
      //   Toast.fail('摄像头数量不能为空')
      // } else if (this.videoTotal === '') {
      //   Toast.fail('摄像头总额不能为空')
      // } else if (this.memory === '') {
      //   Toast.fail('内存卡数量不能为空')
      // } else if (this.memoryTotal === '') {
      //   Toast.fail('内存卡总额不能为空')
      // }
    // else {
      let params = {
        Id: this.projectId,
        WZId: window.localStorage.getItem('UserId'),
        CompanyName: this.companyName,
        LinkName: this.userName,
        LinkPhone: this.myTrim(this.userNumber, 'g'),
        Province: this.cityCode[0],
        City: this.cityCode[1],
        Address: this.addressDetail,
        Service: this.service || 0,
        ServiceTotal: this.serviceTotal || 0,
        Video: this.video || 0,
        VideoTotal: this.videoTotal || 0,
        Memory: this.memory || 0,
        MemoryTotal: this.memoryTotal || 0,
        ApplyStatus: applyStatus
      }
      let response = await post(api.EDITWZPROJECT, params)
      if (response.Success) {
        this.props.history.push(`/prompt?${state}`)
      } else {
        Toast.fail(response.Message)
      }
    }
  }

  // 提交项目
  @action handleSubmitProject = async (state) => {
    this.common(1, state)
  }

  // 保存为洽谈中的项目
  @action handleSaveProject = () => {
    this.common(0)
  }

  @action PageInitialization = async () => {
    Load.loading(1, '加载中')
    let responsePrice = await post(api.PUBLICINFO)
    if (responsePrice.Success) {
      // this.servicePrice = responsePrice.Value.UnitPrice
      // this.videoPrice = responsePrice.Value.GoodsList[0].GoodDevice
      // this.memoryPrice = responsePrice.Value.GoodsList[1].GoodDevice
      // this.byDays = (this.service * this.servicePrice).toFixed(2)
      // this.byCount = (this.video * this.videoPrice).toFixed(2)
      // this.bySheets = (this.memory * this.memoryPrice).toFixed(2)
    }
    let response = await post(api.AREALIST)
    if (response.Success) {
      this.total = (Number(this.serviceTotal) + Number(this.videoTotal) + Number(this.memoryTotal)).toFixed(2)
      Load.loading(2, '加载完成')
      response.Value.map(item => {
        this.addressList.push(new AddressModel(item))
      })
    }
  }
}
